﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class ThamSoDAO:AbstractDAO
    {
        public static long laySoKhachToiDa()
        {
            OleDbConnection ketNoi = null;
            long soKhachToiDa=0;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT SoKhachToiDa FROM ThamSo";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbDataReader boDoc = lenh.ExecuteReader();
                while (boDoc.Read())
                {
                    soKhachToiDa = boDoc.GetInt32(0);
                    
                }

            }
            catch
            {
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return soKhachToiDa;
        }
        public static double layHeSoKhachHang()
        {
            OleDbConnection ketNoi = null;
            double heSoKhachHang= 0;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT HeSoKhachHang FROM ThamSo";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbDataReader boDoc = lenh.ExecuteReader();
                while (boDoc.Read())
                {
                    heSoKhachHang = boDoc.GetDouble(0);

                }

            }
            catch
            {
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return heSoKhachHang;
        }
        public static double layPhuThu()
        {
            OleDbConnection ketNoi = null;
            double phuThu = 0;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT PhuThu FROM ThamSo";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbDataReader boDoc = lenh.ExecuteReader();
                while (boDoc.Read())
                {
                    phuThu = boDoc.GetDouble(0);

                }

            }
            catch
            {
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return phuThu;
        }
        public static bool thayDoiSoKhachToiDa(long soKhachMoi)
        {
            bool ketQua = true;
            OleDbConnection ketNoi = null;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "UPDATE ThamSo SET SoKhachToiDa=@SoKhachToiDa ";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);
                OleDbParameter thamSo;

                thamSo = new OleDbParameter("@SoKhachToiDa", OleDbType.Integer);
                thamSo.Value = soKhachMoi;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                ketQua = false;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ketQua;
        }
        public static bool thayHeSoKhachHang(double heSoMoi)
        {
            bool ketQua = true;
            OleDbConnection ketNoi = null;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "UPDATE ThamSo SET HeSoKhachHang=@HeSoKhachHang ";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);
                OleDbParameter thamSo;

                thamSo = new OleDbParameter("@HeSoKhachHang", OleDbType.Double);
                thamSo.Value = heSoMoi;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                ketQua = false;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ketQua;
        }
        public static bool thayPhuThu(double phuThuMoi)
        {
            bool ketQua = true;
            OleDbConnection ketNoi = null;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "UPDATE ThamSo SET PhuThu=@PhuThu ";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);
                OleDbParameter thamSo;

                thamSo = new OleDbParameter("@PhuThu", OleDbType.Double);
                thamSo.Value = phuThuMoi;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                ketQua = false;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ketQua;
        }
    }
}
